//
// Created by alexa on 5/11/2024.
//

#include "MainMenu.h"
#include "../../keydown-handler/KeydownHandler.h"
#include <iostream>
#include <conio.h>

using namespace std;

void MainMenu::printMain() {
    clearScreen();
    printKeybinds();
    cout << (*t)("title", "main") << endl;
    cout << "1. " << (*t)("option1", "main") << endl;
    cout << "2. " << (*t)("option2", "main") << endl;
    cout << "3. " << (*t)("option3", "main") << endl;
    cout << "4. " << (*t)("option4", "main") << endl;
    cout << "5. " << (*t)("option5", "main") << endl;
    cout << "6. " << (*t)("option6", "main") << endl;
    cout << (*t)("select-action", "general",
                 {
                         {"min", "1"},
                         {"max", "6"}
                 });
}

MainMenu::MainMenu(i18n::Translator *t, UserInterface *ui, MainService *mainService) : Menu(t, ui),
                                                                                       mainService(mainService) {}

void MainMenu::displayMenu() {
    printMain();
    return keydownHandle(ui->getPressedKey, {
            {Keys::TAB,   [this]() {
                ui->toggleLocale();
                return displayMenu();
            }},
            {Keys::ESC,   [this]() {
                ui->setCurrentPage(Pages::PREVIEW_MENU);
                return;
            }},
            {Keys::KEY_1, [this]() {
                ui->setCurrentPage(Pages::BATTALIONS_MENU);
                return;
            }},
            {Keys::KEY_2, [this]() {
                ui->setCurrentPage(Pages::UNITS_MENU);
                return;
            }},
            {Keys::KEY_3, [this]() {
                ui->setCurrentPage(Pages::CREWS_MENU);
                return;
            }},
            {Keys::KEY_4, [this]() {
                ui->setCurrentPage(
                        Pages::SOLDIERS_MENU);
                return;
            }},
            {Keys::KEY_5, [this]() {
                ui->setCurrentPage(
                        Pages::MISSILES_MENU);
                return;
            }},
            {Keys::KEY_6, [this]() {
                changeDir();
            }}
    });
}

void MainMenu::changeDir() {
    cout << endl << (*t)("enter-dir", "main") << endl;
    string newDirPath;
    getline(cin, newDirPath);
    mainService->setDefaultDataDirectory(newDirPath);
    cout << (*t)("dir-changed", "main") << endl;
    cout << (*t)("continue", "general") << endl;
    _getch();
    return displayMenu();
}
